/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.yournamehere.client;

import com.google.gwt.user.client.ui.*;
import java.util.HashMap;

/**
 *
 * @author D4v1D
 */
public class SudokuInputDialog extends DialogBox{
    private SudokuPanel p;
    private int row;
    private int col;
    private TextBox tb;
    public class MyClickListener implements ClickListener{
        private SudokuPanel p;
        private TextBox tb;
        private int row;
        private int col;
        public MyClickListener(SudokuPanel p,int row,int col,TextBox tb){
            super();
            this.p=p;
            this.row=row;
            this.col=col;
            this.tb=tb;
        }
        public void onClick(Widget sender) {
            String value = tb.getText();
            if(value.equals("0")){
                p.setText(row, col,"");
            }else{
                if(p.checkValidity(value,row, col)){
                    p.setText(row, col, value);
                }
            }
            SudokuInputDialog.this.hide();
        }
    }
    public SudokuInputDialog(SudokuPanel p,int row,int col) {
        // Set the dialog box's caption.
        this.p=p;
        this.row=row;
        this.col=col;
        setText("Valeur");
        this.addStyleName("dialog");
        this.setPopupPosition(450, 70);
        this.tb = new TextBox();
        tb.addKeyboardListener(new KeyboardListener() {
            public void onKeyDown(Widget sender, char keyCode, int modifiers) {
            }
            public void onKeyPress(Widget sender, char keyCode, int modifiers) {
                if(keyCode==KEY_ENTER){
                    String value = SudokuInputDialog.this.tb.getText();
                    String oldValue=SudokuInputDialog.this.p.getText(SudokuInputDialog.this.row,SudokuInputDialog.this.col);
                    SudokuInputDialog.this.p.setText(SudokuInputDialog.this.row, SudokuInputDialog.this.col, value);
                    if(!SudokuInputDialog.this.p.checkValidity(value,SudokuInputDialog.this.row, SudokuInputDialog.this.col)){
                        SudokuInputDialog.this.p.setText(SudokuInputDialog.this.row, SudokuInputDialog.this.col, oldValue);
                    }
                    SudokuInputDialog.this.hide();
                }
            }

            public void onKeyUp(Widget sender, char keyCode, int modifiers) {
                
            }
        });
        setWidget(tb);

        // DialogBox is a SimplePanel, so you have to set its widget property to
        // whatever you want its contents to be.
        Button ok = new Button("OK");
        ok.addClickListener(new MyClickListener(p,row,col,tb));
    }

}
